#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    stringModel = new QStringListModel(this);

    QStringList stringList;
    stringList << "itme1" << "itme2" << "itme3" << "itme4" << "itme5" << "itme6";
    //为model设置数据项
    stringModel->setStringList(stringList);

    //view视图设置model
    ui->listView->setModel(stringModel);
    ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_btnTextClear_clicked()
{
    ui->plainTextEdit->clear();
}

void Widget::on_btnTextImport_clicked()
{
    ui->plainTextEdit->clear();

    QStringList strLists = stringModel->stringList();
    foreach(auto str, strLists) {
        ui->plainTextEdit->appendPlainText(str);
    }
}

void Widget::on_btnIniList_clicked()
{
    QStringList stringList;
    stringList << "itme1" << "itme2" << "itme3" << "itme4" << "itme5" << "itme6";
    //为model设置数据项
    stringModel->setStringList(stringList);
}

void Widget::on_btnListClear_clicked()
{
    stringModel->removeRows(0, stringModel->rowCount());
}

void Widget::on_btnListDelete_clicked()
{
    stringModel->removeRow(ui->listView->currentIndex().row());
}

//添加model数据项item
void Widget::on_btnListAppend_clicked()
{
    stringModel->insertRow(stringModel->rowCount());
    QModelIndex index = stringModel->index(stringModel->rowCount() - 1, 0);
    stringModel->setData(index, "new item", Qt::DisplayRole);
    ui->listView->setCurrentIndex(index);
}

//插入model数据项item
void Widget::on_btnListInsert_clicked()
{
    QModelIndex index = ui->listView->currentIndex();
    stringModel->insertRow(index.row());
    stringModel->setData(index, "insert item", Qt::DisplayRole);
    ui->listView->setCurrentIndex(index);
}
